package NLP.impl;

import NLP.GrammarCheck;
import entity.Word;

import java.util.List;

/**
 * @author: ChenforCode
 * @date: 2018/12/28 22:42
 * @description: 检查诗是否符合韵律，语法，平仄三方面要求
 */
public class GrammarCheckImpl implements GrammarCheck {
    //定义五言绝句的四种平仄格式，平0 仄1
    private static final String level1 = "11001001100001111100";
    private static final String level2 = "00011111001100100110";
    private static final String level3 = "11100001100001111100";
    private static final String level4 = "00110111001100100110";

    //只要保证第二句末尾和第四句末尾的韵脚相同即可，第一句可押韵也可不押韵
    public boolean checkRhythm(List<Word> poem) {
        int wordLength = 0;
        String yunmu_2 = "";
        String yunmu_4 = "";
        for (Word w :
                poem) {
            wordLength += w.length();
            if (10 == wordLength) {
                yunmu_2 = w.getYunmu().get(w.getYunmu().size() - 1);
            }
            if (20 == wordLength) {
                yunmu_4 = w.getYunmu().get(w.getYunmu().size() - 1);
            }
        }
        return yunmu_2.equals(yunmu_4);
    }

    public boolean checkGrammar(List<Word> poem) {
        return true;
    }

    //取出所有字词的平仄，与四种level相对比
    public boolean checkLevelAndOblique(List<Word> peom) {
        String level = "";
        for (Word w :
                peom) {
            List<String> tones = w.getTone();
            for (String tone :
                    tones) {
                if ("1".equals(tone) || "2".equals(tone)) {
                    level += "0";
                } else {
                    level += "1";
                }
            }
        }

        return level.equals(level1) || level.equals(level2) || level.equals(level3) || level.equals(level4);
    }
}
